package crudfx.control;
import crudfx.interop.*;
import crudfx.data.*;
import crudfx.util.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import javafx.async.*;
import javax.swing.text.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class MiniBrowser extends UIComponent{
    var jScrollPane:JScrollPane;
    var jEditorPane:JEditorPane;
    //var kit:HTMLEditorKit;
    //var urls:String[];
    var lock:Boolean=false;
    public var hoverURL:String;
    public var currentURL:String on replace o=n{
        refresh();
        }
    /*public function back(){
        var i:Integer=sizeof urls;
        if(i>1){
            var p:String=urls[i-2];
            currentURL=p;
            delete urls[i];
            delete urls[i-1];
            }
        }*/
    public function refresh(){
        if(lock){println("lock!!!");return;}
        //jEditorPane.setVisible(false);
        //println("refresh {currentURL}");
        lock=true;
        var url:String=currentURL;
        go(url);
        lock=false;
        //jEditorPane.setVisible(true);
        /*jEditorPane.setVisible(false);
        lock=true;
        Waiter{
            action:function():Void{go(url);}
            onDone:function():Void{
                jEditorPane.setVisible(true);
                lock=false;
                println("done waiter");
                }
            delay:1s
            }.serve();*/
        //println("done refresh");
        }
    /*function _go(n:String):Void{
        println("go {n}");
        /*var task:SubTask=SubTask{
            url:new URL(n)
            jEditorPane:jEditorPane
            };
        task.start();*/
        //FX.deferAction(function() {
            //jEditorPane.setPage(new URL(n));
            //});


        
        //}
    function go(n:String):Void{
        try{
           // var t:JEditorPane=new JEditorPane(new URL(n));
            //println(t.getDocument());
            /*var t:JEditorPane=new JEditorPane(new URL(n));
            jScrollPane.getViewport().removeAll();
            t.setEditable(false);
            jScrollPane.getViewport().add(t);
            jEditorPane=t;*/
            //insert n into urls;
            //println("before {(jEditorPane.getDocument() as HTMLDocument).getAsynchronousLoadPriority()}");
            
            
            
            //jEditorPane.setPage(new URL(n));

            jScrollPane.getViewport().removeAll();
            jEditorPane=new JEditorPane(new URL(n));
            var documentListener:DocumentListener=DocumentListener{
                override public function insertUpdate(e:DocumentEvent):Void{
                    println("insertUpdate: {e}");
                    }
                override public function removeUpdate(e:DocumentEvent):Void{
                    println("removeUpdate: {e}");
                    }
                override public function changedUpdate(e:DocumentEvent):Void{
                    println("changedUpdate: {e}");
                    }
                };
            jEditorPane.getDocument().addDocumentListener(documentListener);
            //addPane();


            //HyperlinkEvent.setDocument(Tools.getHTMLDocument(new URL(n)));
            //println("after {(jEditorPane.getDocument() as HTMLDocument).getAsynchronousLoadPriority()}");


            /*var font:Font = UIManager.getFont("Label.font");
            var bodyRule:String = "body \{ font-family: {font.getFamily()}; font-size: { font.getSize() }pt; \}";
            (jEditorPane.getDocument() as HTMLDocument).getStyleSheet().addRule(bodyRule);
            */

            //println("done set");
        }catch(x){
            x.printStackTrace();
            }        
        };
    function addPane():Void{
        jEditorPane.setEditable(false);
        var hyperlinkListener:HyperlinkListener=HyperlinkListener{
            override public function hyperlinkUpdate(he:HyperlinkEvent):Void{
                println(he);
                var type:javax.swing.event.HyperlinkEvent.EventType=he.getEventType();
                if(type==javax.swing.event.HyperlinkEvent.EventType.ENTERED){
                    hoverURL=he.getURL().toString();
                    }
                else{
                    if(type==javax.swing.event.HyperlinkEvent.EventType.EXITED){
                        hoverURL="";
                        }
                    }
                }
            };
        jEditorPane.addHyperlinkListener(hyperlinkListener);
        jScrollPane.getViewport().add(jEditorPane);
        }
    init{
        jScrollPane=new JScrollPane();
        //jEditorPane=new SEditorPane();
        /*kit=HTMLEditorKit{
            override public function createDefaultDocument():Document{
                var doc:Document=super.createDefaultDocument();
                try{ (doc as HTMLDocument).setAsynchronousLoadPriority(-1);}
                catch(x){ x.printStackTrace(); }
                return doc;
                }
            };*/
        //jEditorPane.setEditorKit(kit);
        /*jScrollPane.getViewport().add(jEditorPane);
        //jEditorPane.setEditable(false);
        var hyperlinkListener:HyperlinkListener=HyperlinkListener{
            override public function hyperlinkUpdate(he:HyperlinkEvent):Void{
                println(he);
                var type:javax.swing.event.HyperlinkEvent.EventType=he.getEventType();
                //println("{type} - {javax.swing.event.HyperlinkEvent.EventType.ENTERED}");
                if(type==javax.swing.event.HyperlinkEvent.EventType.ENTERED){
                    //println("enter");
                    hoverURL=he.getURL().toString();
                    }
                else{
                    if(type==javax.swing.event.HyperlinkEvent.EventType.EXITED){
                        //println("exit");
                        hoverURL="";
                        }
                    }
                //println(hoverURL);
                }
            };
        jEditorPane.addHyperlinkListener(hyperlinkListener);*/
        }
    override public function getJComponent():JComponent{
        return jScrollPane;
        }
    override public function highlight(s:String):Boolean{
        return false;
        }
    override public function gather():DataTree{
        var me:DataTree=DataTree{ name:"MiniBrowser" };
        return me;
        }
    }
